package cn.edu.swu.safety;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;

@WebServlet("/Code")
public class AuthCode extends HttpServlet {

    public final static String AUTH_CODE="AUTH_CODE";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShearCaptcha shearCaptcha= CaptchaUtil.createShearCaptcha(60,30,4,4);
        String code= shearCaptcha.getCode(); //获取验证码的值
        req.getSession(true).setAttribute(AUTH_CODE,code);

        try(OutputStream outputStream=resp.getOutputStream()){
            shearCaptcha.write(outputStream);
        }
    }
}
